package com.shmilyou.dissertationmanagement.biz;
/* Created with 岂止是一丝涟漪     530060499@qq.com    2019/2/9 */

import com.shmilyou.dissertationmanagement.domain.Patent;
import com.shmilyou.dissertationmanagement.repository.PatentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
public class PatentService {

    private final PatentRepository patentRepository;

    @Autowired
    public PatentService(PatentRepository patentRepository) {
        this.patentRepository = patentRepository;
    }

    public Patent queryById(Integer id) {
        Optional<Patent> patent = patentRepository.findById(id);
        return patent.orElse(null);
    }

    @Transactional
    public Patent save(Patent entity) {
        Patent save = patentRepository.save(entity);
        return save;
    }

    public Patent update(Patent entity) {
        return patentRepository.save(entity);
    }

    public Patent delete(Patent entity) {
        entity.setDel(1);
        return patentRepository.save(entity);
    }

    public List<Patent> queryByName(String name) {
        return patentRepository.findAllByName(name);
    }

    public List<Patent> queryAll() {
        return patentRepository.findAllByDel(0);
    }

    public Page<Patent> queryAll(int pageIndex, int pageSize) {
        Pageable page = PageRequest.of(pageIndex, pageSize);
        Page<Patent> content = patentRepository.findAllByDel(page, 0);
        return content;
    }

    public int getCount() {
        return patentRepository.countByDel(0);
    }
}
